
import { Init } from '/init.js';
const init = new Init();

const loginUrl = 'https://scrm.bkchina.cn/scrm-bk-jd/jdUserController/onLogin';
const authCodeUrl = 'https://scrm.bkchina.cn/scrm-bk-jd/jdTokenController/getAccessToken';
// 获取用户信息
const authUserInfoUrl = 'https://scrm.bkchina.cn/scrm-bk-jd/jdUserController/submitUserInfo?';

App({
  onLaunch: function () {
	  // jd.clearStorageSync();
  },
  
  onShow: function() {
	  
  },
  
  // 将用户信息提交到后台
  getJdUserInfo: function() {
    // 查看是否授权
    let _that = this;
    return new Promise((resolve, reject) => {
		jd.getSetting({
		  success: function(res) {
			  if(res.authSetting['scope.userInfo']) {
				  jd.getUserInfo({
					  withCredentials: true,
					  success: function(res) {
						  console.log("获取用户信息");
						  console.log(res);
						  _that.globalData.userInfo = res;
					  }
				  })
			  }
		   }
		});
	});
  },
  
  // 获取用户openId
  getUserOpenId: function() {
	  const _that = this;
	  return new Promise((resolve, reject) => {
		  jd.login({
		  	success: function(res) {
		  		const getJdUserOauthInfo = init.handleGetUserOpenId(res.code, loginUrl);
		  		getJdUserOauthInfo.then((res) => {
		  			if(res.code == 200) {
					  if(res.openId) {
						  console.log("获取用户openId");
						  console.log(res);
						  // openId放入全局变量
						  _that.globalData.openId = res.openId;
						  jd.setStorageSync('openId', res.openId);
						  console.log("----获取用户当前设置----");
						  // 调用获取用户信息接口
						  jd.getSetting({
						    success: function(res) {
							  console.log("===查看用户信息授权接口===");
							  console.log(res);
						  	  if(res.authSetting['scope.userInfo']) {
								  console.log("===authSetting 为true=====");
						  		  jd.getUserInfo({
									  withCredentials: true,
									  success: function(res) {
										  _that.globalData.userInfo = res.userInfo;
										  console.log("第一步获取用户信息");
										  console.log(res);
										  resolve(res);
									  }
									})
								} else {
									console.log("===authSetting 为false=====");
									jd.getUserInfo({
										withCredentials: true,
										success: function(res) {
										   _that.globalData.userInfo = res.userInfo;
										   console.log("===authSetting为false====获取用户信息===");
										   console.log(res);
										   resolve(res);
										},
										fail: function(e) {
											console.log("====当用户未授权过===报错===");
											console.log(JSON.stringify(e));
											jd.redirectTo({
												url: '/pages/authorize/index'
											});
										}
									})
								}
						     }
						  });
					  }
					  if(res.unionId) {
						  _that.globalData.unionId = res.unionId;
						  jd.setStorageSync('unionId', res.unionId)
					  }
		  			}
		  		});
		  	}
		  })
	  });
  },
  
  // 获取用户token
  getUserAccessToken: function() {
	  console.log("===进入获取用户accesstoken====方法");
	  const _that = this;
	  return new Promise((resolve, reject) => {
		  jd.getAuthCode({
		    success: function(res) {
			  console.log("====成功进入获取Authcode方法内=====");
		  	  const getJdUserAccessToken = init._handleGetAccessToken(res.code, authCodeUrl);
		  	  getJdUserAccessToken.then((res) => {
		  		  if(res && res.access_token) {
					  console.log("获取用户token");
					  console.log(res);
					  _that.globalData.accessToken = res.access_token;
		  			  jd.setStorageSync('access_token', res.access_token);
					  console.log("在app.js中获取用户token");
					  console.log(jd.getStorageSync('access_token'));
					  resolve(res);
		  		  }
		  	  });
		     }
		  })
	  })
  },
  
  // 全局参数
  globalData: {
    userInfo: null,
	openId: null,
	unionId: null,
	accessToken: null,
	isSubmitMemberInfo: false
  }
  
});